package com.wang.rabbitmqstudy.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 交换机—队列绑定配置
 */
@Configuration
public class BindingConfig {
    @Autowired
    private ExchangeConfig exchangeConfig;
    @Autowired
    private QueueConfig queueConfig;
    @Autowired
    private PropertiesConfig propertiesConfig;

    /**
     * 直连绑定
     */
    @Bean
    Binding directBinding(){
        return BindingBuilder.bind(queueConfig.redQueue()).to(exchangeConfig.studyDirectExchange()).with(propertiesConfig.getRedRoutingKey());
    }

    /**
     * 主题绑定1
     */
    @Bean
    Binding blueTopicBinding(){
        return BindingBuilder.bind(queueConfig.blueQueue()).to(exchangeConfig.studyTopicExchange()).with(propertiesConfig.getBlueTopicRoutingKey());
    }

    /**
     * 主题绑定2
     */
    @Bean
    Binding yellowTopicBinding(){
        return BindingBuilder.bind(queueConfig.blueQueue()).to(exchangeConfig.studyTopicExchange()).with(propertiesConfig.getYellowTopicRoutingKey());
    }

    /**
     * 广播绑定1
     */
    @Bean
    Binding redFanoutBinding(){
        return BindingBuilder.bind(queueConfig.redQueue()).to(exchangeConfig.studyFanoutExchange());
    }
    /**
     * 广播绑定2
     */
    @Bean
    Binding blueFanoutBinding(){
        return BindingBuilder.bind(queueConfig.blueQueue()).to(exchangeConfig.studyFanoutExchange());
    }
    /**
     * 广播绑定3
     */
    @Bean
    Binding yellowFanoutBinding(){
        return BindingBuilder.bind(queueConfig.yellowQueue()).to(exchangeConfig.studyFanoutExchange());
    }

    //=================== 参数测试 ==========================
    /**
     * 排他队列绑定
     */
    @Bean
    Binding argumentsExclusiveBinding(){
        return BindingBuilder.bind(queueConfig.exclusiveQueue()).to(exchangeConfig.argumentsExchange()).with(propertiesConfig.getArgumentsExclusiveRoutingKey());
    }
    /**
     * 自动删除队列绑定
     */
    @Bean
    Binding argumentsAutoDeleteBinding(){
        return BindingBuilder.bind(queueConfig.autoDeleteQueue()).to(exchangeConfig.argumentsExchange()).with(propertiesConfig.getArgumentsAutoDeleteRoutingKey());
    }

    /**
     * 优先级队列绑定
     */
    @Bean
    Binding argumentsPriorityBinding(){
        return BindingBuilder.bind(queueConfig.priorityQueue()).to(exchangeConfig.argumentsExchange()).with(propertiesConfig.getArgumentsPriorityRoutingKey());
    }

    /**
     * ttl队列绑定
     */
    @Bean
    Binding argumentsTtlBinding(){
        return BindingBuilder.bind(queueConfig.ttlQueue()).to(exchangeConfig.argumentsExchange()).with(propertiesConfig.getArgumentsTtlRoutingKey());
    }

    /**
     * 普通队列绑定
     */
    @Bean
    Binding argumentsCommonBinding(){
        return BindingBuilder.bind(queueConfig.commonQueue()).to(exchangeConfig.argumentsExchange()).with(propertiesConfig.getArgumentsCommonRoutingKey());
    }

    /**
     * 死信队列绑定
     */
    @Bean
    Binding deadLetterBinding(){
        return BindingBuilder.bind(queueConfig.deadLetterQueue()).to(exchangeConfig.deadLetterExchange()).with(propertiesConfig.getDeadLetterRoutingKey());
    }

    /**
     * 设置备用交换机的普通队列
     */
    @Bean
    Binding backupCommonBinding(){
        return BindingBuilder.bind(queueConfig.backupCommonQueue()).to(exchangeConfig.backupCommonExchange()).with(propertiesConfig.getBackupCommonRoutingKey());
    }

    /**
     * 备用交换机绑定
     */
    @Bean
    Binding backupBinding(){
        return BindingBuilder.bind(queueConfig.backupQueue()).to(exchangeConfig.backupExchange()).with("#");
    }

}
